package demo;

/**
 * Remarks:
 * Class name :Thread2
 * Date :2023/2/13 17:27
 * Author:杨俊源
 */

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 多线程实现同步下载图片
 */
public class Thread2_2 implements Runnable{

    private String url;
    private String name;

    public Thread2_2(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public void run() {
        WebDownLoader2 webDownLoader = new WebDownLoader2();
        webDownLoader.download(url,name);
        System.out.println("download  name = " + name);
    }
    public static void main(String[] args) {
        Thread2 thread1 = new Thread2("https://img1.baidu.com/it/u=3306572176,704436528&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500","1.jpg");
        Thread2 thread2 = new Thread2("https://img1.baidu.com/it/u=3306572176,704436528&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500","2.jpg");
        Thread2 thread3 = new Thread2("https://img1.baidu.com/it/u=3306572176,704436528&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500","3.jpg");
        new Thread(thread1).start();
        new Thread(thread2).start();
        new Thread(thread3).start();

    }
}

class WebDownLoader2{
    public void download(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常");
        }
    }
}
